﻿using AutoMapper;
using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interfance;
using B.S.BasicData.Read.Apilication.Common.MenuRole;
using B.S.BasicData.Read.DTOS;
using MediatR;

namespace B.S.BasicData.Read.Apilication.Handler.MenuRole
{
    public class GetRoleCommandHandle:IRequestHandler<GetRoleCommand,ApiResult<List<RoleDTO>>>
    {
        private readonly IBaseRepository<RoleModel> RoleDal;
        private readonly IMapper mapper;

        public GetRoleCommandHandle(IBaseRepository<RoleModel> roleDal, IMapper mapper)
        {
            RoleDal = roleDal;
            this.mapper = mapper;
        }

        public Task<ApiResult<List<RoleDTO>>> Handle(GetRoleCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<RoleDTO>> result = new ApiResult<List<RoleDTO>>();
            var list = RoleDal.GetAll().Where(x => x.IsDeleted == false).Select(x => new RoleDTO()
            {
                RoleId = x.RoleId,
                RoleName = x.RoleName,
            }).ToList();

            result.Code = ApiEnum.Success;
            result.Message = ApiEnum.Success.ToString();
            result.Data = list;
            return Task.FromResult(result);
            //result.Code = ApiEnum.Success;
            //result.Message = "角色树形结构显示成功";
            //result.Data = gettree(0);
            //return Task.FromResult(result);
        }

        //private List<RoleDTO> gettree(int parment)
        //{
        //    var info = RoleDal.GetAll().Where(x => x.ParentId == parment).ToList();
        //    var model = mapper.Map<List<MenuRoleDto>>(info);
        //    foreach (var item in model)
        //    {
        //        item.Childer = gettree(item.MenuId);
        //    }
        //    return model;
        //}


    }
}
